package com.test.controllers;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/file")
public class FileUploaderController {

    @PostMapping("/upload")
    public List<Map<String, String>> upload(final HttpServletRequest request){
        final MultipartHttpServletRequest multipartReq = (MultipartHttpServletRequest) request;
        List<MultipartFile> fileRes = multipartReq.getFiles("file");
        if(null != fileRes) {
            return fileRes.stream().map(item -> {
                Map<String, String> map = new HashMap<>();
                map.put("name", item.getName());
                map.put("id", UUID.randomUUID().toString());
                map.put("url",
                        "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100");
                return map;
            }).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
